package com.cgah

import grails.test.*

class CustomerServiceTests extends AbstractIntegrationTest {
   public void setUp() {
        super.setUp()
    }

    public void tearDown() {
        super.tearDown()
    }

    // S-1: Create a Grails service method that supports creating a new customer (integration test)
    void testCreate_ReturnsCorrectObject() {
        params.username = "buyer"
        params.emailAddress = "buyer@test.com"
        params.password = "boooya"
        params.createdDate = new Date()
        params.enabled = true

        def customerService = new CustomerService();

        def customer = customerService.create(params)
        print "result is:" + customer;
        assertEquals(customer.username, params.username);
        assertEquals(customer.emailAddress, params.emailAddress);
        assertEquals(customer.password, params.password);
        assertEquals(customer.createdDate, params.createdDate);
        assertEquals(customer.enabled, params.enabled);
    }

    // S-1: Create a Grails service method that supports creating a new customer (integration test)
    void testSave_ObjectIsPersisted() {
        params.username = "buyer"
        params.emailAddress = "buyer@test.com"
        params.password = "boooya"
        params.createdDate = new Date()
        params.enabled = true

        def customerService = new CustomerService();

        def customer = customerService.create(params)
        print "result is:" + customer;
        // on create the object is not persisted to the DB, hence there is no ID
        assertNull(customer.id);
        customer = customerService.save(customer);
        // on save, the object gets persisted to the DB, hence there is an ID.
        // this should be all we need to do, since the ID is only ever assigned once the object is persisted
        // successfully to the DB AND in theory, no one should be able to set the ID.
        assertNotNull(customer.id);
        assertEquals(customer.username, params.username);
        assertEquals(customer.emailAddress, params.emailAddress);
        assertEquals(customer.password, params.password);
        assertEquals(customer.createdDate, params.createdDate);
        assertEquals(customer.enabled, params.enabled);
    }


    
}
